<?php

class Cleverweb {
	public function __construct() {
	   add_action( 'wp_enqueue_scripts', array(&$this, 'enqueue_scripts'), 20 );
	   add_shortcode( 'test', array(&$this, 'test_func'));
	   
	}
	
	public function test_func( $atts ){
	   global $wpdb;
	   
		echo '<form id="findBootsByPlayer">
				Spillernavn: <input type="search" name="playername">
				<input type="button" value="Søg">
				</form>
				<div id="results">
				</div>';

//		$mysqli = new mysqli('localhost', 'root', '', 'fodboldfessor_dk_db');

		/*
		 * This is the "official" OO way to do it,
		 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
		 */
//		if ($mysqli->connect_error) {
//		    die('Connect Error (' . $mysqli->connect_errno . ') '
//		            . $mysqli->connect_error);
//		}

		/*
		 * Use this instead of $connect_error if you need to ensure
		 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
		 */
//		if (mysqli_connect_error()) {
//		    die('Connect Error (' . mysqli_connect_errno() . ') '
//		            . mysqli_connect_error());
//		}

		$result = $wpdb->get_results('SELECT * FROM `ff_wp_players`');
//		dump($result);
		$count = 0;

		?>
		<script type="text/javascript" >
		var playersBoots = {};
		<?php

		foreach ($result as $player) {
//		   echo $player->name;
	        $playername = $player->name;
	        $bootname = $player->boot;

	        echo 'playersBoots['.$count.'] = {};';

	        foreach($player as $property => $value)
	        {
	        	if ($property != 'html')
	        		echo 'playersBoots['.$count.'].'.$property.' = "'.$value.'";'; 
	        }

	        echo "\n";
	        								
	    	
	        $count++;
	    }
	    echo '</script>';
//		$mysqli->close();
	}

	
	/**
	 * Adds new stylesheets and scripts
	 * 
	 */
	public function enqueue_scripts()
	{
	   wp_enqueue_style( 'dashicons' );
	   wp_enqueue_style( 'cw-main', CW_ASSETS_URL.'css/main.css', array(), '1.0' );
	   
	   wp_enqueue_script( 'cw-main', CW_ASSETS_URL.'js/main.js', array( 'jquery' ), '1.0' );
	}
}

new Cleverweb();